<script>
    $(document).ready(function(){
        $('.isExpired').each(function(){
            $(this).parent().parent().addClass('expired-invoice');
        })
        
        $('.noExpired').each(function(){
            $(this).parent().parent().addClass('no-expired-invoice');
        })
    })
</script>
<style>
    .expired-invoice{
        background-color:red;
    }
    .no-expired-invoice{
        background-color:yellow;
    }
    
</style>

<?php

$html='';

foreach ($data as $stateName => $stateData)
{
    $header = '<table width="1300" style="" cellspacing="0" cellpadding="2">
            <tbody><tr>
<th colspan="9" align="left" style="background-color: #888888;width:100%;border: 1px solid black;">
  <b style="background-color: #888888;border: 1px double black;"> ' . $stateName . ' </b>  </th> </tr></tbody></table>';

    $body = '';
    $table = new CI_Table();
    $table->set_template(array(
        'table_open'=>'<table width="1300" border="1">'
    ));
    $table->add_row(
        array(
            'data' => '<b>Contract</b>',
            'style' => 'border-bottom: 2px solid  black;'
        ), array(
            'data' => '<b>Start</b>',
            'style' => 'border-bottom: 2px solid  black;',
        ), array(
            'data' => '<b>End</b>',
            'style' => 'border-bottom: 2px solid  black;'
        ), array(
            'data'=>'<b>Invoice</b>',
            'style'=>'border-bottom: 2px solid  black;',
        ),array(
            'data'=>'<b>County</b>',
            'style'=>'border-bottom: 2px solid  black;',
        ),array(
            'data'=>'<b>Dept</b>',
            'style'=>'border-bottom: 2px solid  black;',
        ),array(
            'data'=>'<b>Custno - Locno - Locname</b>',
            'style'=>'border-bottom: 2px solid  black;'
        ),array(
            'data'=>'<b>Earned Revenue</b>',
            'align'=>'right',
            'style'=>'border-bottom: 2px solid  black;text-align:right;',
        ),array(
            'data'=>'<b>Amount</b>',
            'align'=>'right',
            'style'=>'border-bottom: 2px solid  black;text-align:right;'
        )
                
           
    );
    //'class'=>$item['isExpired']==true?'expired':null
    foreach ($stateData['items'] as $i => $item)
    {
        $expiredSign=null;
        if($item['isExpired']==true){
            $expiredSign='<span class="isExpired"></span>';
        }else{
            $expiredSign='<span class="noExpired"></span>';
        }
        $table->add_row(
                $item['contractid'].$expiredSign,
                $item['contractstartdate'],
                $item['contractenddate'],             
                $item['invoice'],
                $item['county'],
                $item['department'],
                array(
                    'data'=>$item['custno'].' - '.$item['locno'].' - '.$item['locname'],               
                    //'colspan'=>4,
                ),
                
                array(
                    'data'=>number_format($item['revenue'],2),
                    'style'=>'text-align:right;'
                ),
                array(
                    'data'=>number_format($item['contractamount'],2),
                    'style'=>'text-align:right;'
                )                
           );
        
        //revenue per month
        
        if(isset($item['revenueByMonths']) && count($item['revenueByMonths'])>0){     
            $monthTable=new CI_Table();
            $monthTable->set_template(array(
                'table_open'=>'<table width="900" border="1">'
            ));
            $monthRows=array();
            for($i=1;$i<=12;$i++){
                $monthRows[]=date('M',strtotime("2012-$i-1"));            
            }            
            $monthTable->add_row($monthRows);
            
            $rows=array();
            for($i=1;$i<=12;$i++)
            {
                $rows[]=array(
                    'data'=>number_format($item['revenueByMonths'][$i],2),
                    //'style'=>'text-align:right;'
                    );
            }
            $monthTable->add_row($rows);
            
            $rows2=array();
            if(count($item['revenueByMonths'])==24)
            {
                for($i=13;$i<=24;$i++){
                    $rows2[]=array(
                        'data'=>number_format($item['revenueByMonths'][$i],2),
                        //'style'=>'text-align:right;'
                    );
                }
                $monthTable->add_row($rows2);
            }
            $table->add_row(
                array(
                    'data'=>'&nbsp'
                ),    
                array(
                    'data'=>$monthTable->generate(),
                    'colspan'=>'8'
                )
            );
        }
        
    }
    $totalTable = new CI_Table();
    $totalTable->set_template(array(
        'table_open'=>'<table width="1300" border="1">'
    ));
    $totalTable->add_row(
            array(
                'data'=>$stateName,
                'style'=>'width: 705px;border-bottom:  1px solid  black;'
            ),
            array(
                'data'=>'Earned Revenue',
                'style'=>'width:75px;border-bottom:  1px solid  black;font-style:italic;text-align:right;',
                'aligh'=>'right'
            ),array(
                'data'=>'Amount',
                'style'=>'width:75px;border-bottom:  1px solid  black;font-style:italic;text-align:right;',
                'aligh'=>'right'
            )
            );
    $totalTable->add_row(
            array(
                'data'=>'Total'
            ),
            array(
                'data'=>number_format($stateData['revenue'],2),
                'style'=>'text-align:right;'
            ),array(
                'data'=>number_format($stateData['amount'],2),
                'style'=>'text-align:right;'
            ));
    
    
    $revenueByMonthsTable = new CI_Table();
    $revenueByMonthsTable->set_template(array(
        'table_open'=>'<table width="1300" border="1">'
    ));
    $monthRows=array();
    for($i=1;$i<=12;$i++){
        
        $monthRows[]=date('M',strtotime("2012-$i-1"));            
    }            
    $revenueByMonthsTable->add_row($monthRows);
    $rows=array();
    foreach($data[$stateName]['revenueByMonths'] as $monthIndex=>$amount){
        if($monthIndex==13){
            $revenueByMonthsTable->add_row($rows);
            $rows=array();
            $rows[]=number_format($amount,2);
        }else{
            $rows[]=number_format($amount,2);
        }
    }
    $revenueByMonthsTable->add_row($rows);
    $html.=$header;
    $html.=$table->generate();
    $html.=$totalTable->generate();            
    $html.=$revenueByMonthsTable->generate();
    $html.='<br/>';
}
$grandTable=new CI_Table();
$grandTable->set_template(array(
        'table_open'=>'<table width="1300" border="1">'
    ));
$grandTable->add_row(
            array(
                'data'=>'Grand',
                'style'=>'width: 705px;border-bottom:  3px solid  black;font-weight:bold'
            ),
            array(
                'data'=>'Earned Revenue',
                'style'=>'width:75px;border-bottom:  3px solid  black;font-style:italic;text-align:right;',
                'aligh'=>'right'
            ),array(
                'data'=>'Amount',
                'style'=>'width:75px;border-bottom:  3px solid  black;font-style:italic;text-align:right;',
                'aligh'=>'right'
            )
            );
    $grandTable->add_row(
            'Total',
            array(
                'data'=>number_format($grandRevenue,2),
                'style'=>'text-align:right;'
            ),
            array(
                'data'=>number_format($grandAmount,2),
                'style'=>'text-align:right;'
            )
            );
    $grandRevenueTable=new CI_Table();
    $grandRevenueTable->set_template(array(
        'table_open'=>'<table width="1300" border="1">'
    ));
    $monthRows=array();    
    for($i=1;$i<=12;$i++){
        
        $monthRows[]=date('M',strtotime("2012-$i-1"));            
    } 
    $grandRevenueTable->add_row($monthRows);
    $rows=array();      
    foreach($grandRevenueByMonths as $monthIndex=>$amount){
        if($monthIndex==13){
           
            $grandRevenueTable->add_row($rows);
            $rows=array();
            $rows[]=number_format($amount,2);
        }else{
            $rows[]=number_format($amount,2);
        }
    }
    $grandRevenueTable->add_row($rows);
    $html.=$grandTable->generate();
    $html.=$grandRevenueTable->generate();
    echo $html;
?>